# -*- coding: utf-8 -*-
from django.conf import settings
from django.template import RequestContext
from django.shortcuts import render_to_response, get_object_or_404
from django.core.paginator import Paginator, InvalidPage, EmptyPage
from django.contrib.flatpages.models import FlatPage
from django.contrib.admin.views.decorators import staff_member_required
from django.views.generic.list_detail import object_detail



from models import News                              

def startpage(request):
    page = FlatPage.objects.filter(url="/")
    if page:
        page = page[0]
    else:
        page = FlatPage("/", "Welcome", "<h1>Hello World!</h1>")
        page.save()
    news = News.active_objects.all()[:5]
    return render_to_response('news/startpage.html',  {"latest_news": news,
                                                       "start_title": page.title, 
                                                       "content": page.content}, 
                              context_instance=RequestContext(request))


@staff_member_required
def preview(request, object_id):
   return object_detail(request, object_id=object_id,
                        queryset=News.objects.all(),
                        template_object_name = 'object', 
                        template_name = "news/detail.html")